package cn.tworice.client.service;

import cn.tworice.common.util.excel.InductsListenerService;
import cn.tworice.api.CrudService;
import cn.tworice.client.dao.ShoppingOrderDao;
import cn.tworice.common.entity.code.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingOrderService implements InductsListenerService<ShoppingOrder>, CrudService<ShoppingOrder> {
    @Autowired
    private ShoppingOrderDao dao;
    @Autowired
    private OrderListService orderListService;

    @Override
    public List<ShoppingOrder> list(Integer page, Integer pageSize, ShoppingOrder entity) {
        return dao.queryList(page * pageSize, pageSize, entity);
    }

    @Override
    public int total(ShoppingOrder entity) {
        return dao.queryTotal(entity);
    }

    @Override
    public int set(ShoppingOrder entity) {
        entity.setUpdateTime(System.currentTimeMillis());
        ShoppingOrder shoppingOrder = dao.queryInfo(entity.getId());
        if (shoppingOrder == null) {
            entity.setCreateTime(System.currentTimeMillis());
            List<OrderList> orderLists = JSON.parseArray(entity.getOrderLists(), OrderList.class);
            orderListService.save(orderLists);
            return dao.insert(entity);
        } else {
            return dao.update(entity);
        }
    }

    @Override
    public boolean update(ShoppingOrder entity) {
        return dao.update(entity) == 1;
    }

    @Override
    public int del(Integer[] ids) {
        return dao.del(ids);
    }

    @Override
    public int save(List<ShoppingOrder> list) {
        list.forEach(item -> {
            item.setCreateTime(System.currentTimeMillis());
            item.setUpdateTime(System.currentTimeMillis());
        });
        return dao.insertList(list);
    }

    @Override
    public ShoppingOrder info(int id) {
        return null;
    }

    public ShoppingOrder info(long id) {
        return dao.queryInfo(id);
    }
}